package com.itheima.bos.web.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.itheima.bos.domain.base.Standard;
import com.itheima.bos.service.StandardService;
import com.itheima.bos.web.action.common.BaseAction;

@ParentPackage("json-default")
@Namespace("/standard")
@Controller
@Scope("prototype")
public class StandardAction extends BaseAction<Standard> {	
	
	@Autowired
	private StandardService standardService;
	
	@Action(value="standard_save",results={@Result(name="success" ,type="redirect" ,location="../pages/base/standard.html")})
	public String standard_save(){
		standardService.save(model);
		return SUCCESS;
	}
	
	@Action(value="standard_validateName",results={@Result(name="success",type="json")})
	public String standard_validateName(){
		List<Standard> list = standardService.standard_validateName(model.getName());
		if(list != null &&  list.size() == 0){
			ServletActionContext.getContext().getValueStack().push(true);
		}else{
			ServletActionContext.getContext().getValueStack().push(false);
		}
		return SUCCESS;
	}

	@Action(value="standard_pageQuery",results={@Result(name="success",type="json")})
	public String standard_pageQuery(){
		Pageable pageable = new PageRequest(page-1, rows);
		Page<Standard> page = standardService.pageQuery(pageable);
		pushPageDateToValueStack(page);
		return SUCCESS;
	}
	
	private String ids;
	
	public void setIds(String ids) {
		this.ids = ids;
	}

	@Action(value="standrad_delete",results={@Result(name="success",type="redirect" ,location="../pages/base/standard.html")})
	public String standrad_delete(){
		standardService.delete(ids);
		return SUCCESS;
	}
	
	@Action(value="standard_findAll",results={@Result(name="success",type="json")})
	public String standard_findAll(){
		List<Standard> list = standardService.findAll();
		ServletActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
}
